package com.it.fastpay.controller;

import com.it.fastpay.service.FastPayService;
import com.it.fastpay.service.Impl.FastPayServiceImpl;
import com.it.fastpay.to.FastPayRedisTo;
import com.it.fastpay.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class FastPayController {


    @Autowired
    private FastPayServiceImpl fastPayService;

    /**
     * 当前时间可以参与秒杀的商品信息
     * @return
     */
    @GetMapping(value = "/getCurrentSeckillSkus")
    @ResponseBody
    public R getCurrentFastPay() {

        //获取到当前可以参加秒杀商品的信息
        List<FastPayRedisTo> vos = fastPayService.getCurrentFastPay();

        return R.ok().setData(vos);
    }


    /**
     * 商品进行秒杀(秒杀开始)
     * @param fastpayId   秒杀id
     * @param key    随机码id
     * @param num    秒杀数量
     * @return
     */
    @GetMapping(value = "/fastpaygoodsIlike")
    public String fastpayGoodsIlike(@RequestParam("fastpayId") String fastpayId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model) {

        String orderSn = null;
        try {
            //1、判断是否登录
            orderSn = fastPayService.fastpayGoodsIlike(fastpayId,key,num);
            model.addAttribute("orderSn",orderSn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }


}

